package util;

import exception.PageException;


public class Page {
	/**
	 *Page工具类，主要用于分页查询，具体见方法注释
	 */
private int totalCount;
private int pageSize=10;
private int pageIndex;
private int totalPage;
private int startRow;//起始记录数
private int endRow;//结束记录数

public Page() {
}

/**
 * 构造方法
 * @param totalCount 待分页数据的总记录数
 * @param pageIndex 当前页码
 * 每页记录数默认为10
 */
public Page(int totalCount, int pageIndex) {
	this.setTotalCount(totalCount);
	calTotalPage(this.pageSize);
	this.setPageIndex(pageIndex);
	setRows(this.pageIndex);
}
/**
 * 构造方法
 * @param totalCount 待分页数据的总记录数
 * @param pageSize 每页显示的记录数
 * @param pageIndex 当前页码
 */
public Page(int totalCount, int pageSize, int pageIndex) {
	this.setPageSize(pageSize);
	this.setTotalCount(totalCount);
	calTotalPage(this.pageSize);
	this.setPageIndex(pageIndex);
	setRows(pageIndex);
}

private void calTotalPage(int pageSize) {
	int totalPage=0;
	if(this.totalCount % pageSize==0){
		totalPage=this.totalCount/pageSize;
	}else{
		totalPage=this.totalCount/pageSize+1;
	}
	this.totalPage=totalPage;
}
public int getTotalCount() {
	return totalCount;
}
public void setTotalCount(int totalCount) {
	if(totalCount<1){
		totalCount=0;
		throw new PageException("总记录数必须大于0！");
	}
	this.totalCount = totalCount;
}
public int getPageSize() {
	return pageSize;
}
public void setPageSize(int pageSize) {
	if(pageSize<1){
		pageSize=10;
		throw new PageException("每页记录数必须大于1！");
	}
	this.pageSize = pageSize;
	calTotalPage(pageSize);
}
public int getPageIndex() {
	return pageIndex;
}
public void setPageIndex(int pageIndex) {
	if(pageIndex<1){
		pageIndex=1;
		throw new PageException("页码必须大于1！");
	}else if(pageIndex>this.totalPage){
		pageIndex=this.totalPage;
		throw new PageException("页码不能大于总页数！");
	}
	this.pageIndex = pageIndex;
	setRows(pageIndex);
}
private void setRows(int pageIndex) {
	
	int startRow=(pageIndex-1)*pageSize+1;
	int endRow=pageIndex*pageSize;
	this.startRow=startRow;
	this.endRow=endRow;
}
public int getStartRow() {
	return startRow;
}
public int getEndRow() {
	return endRow;
}
}
